<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${setMapperPackageName}.${MAPPER_CLASSNAME}">

    <!-- 数据库字段对象关系映射 -->
    <resultMap id="baseResultMap"
        type="${setDomainPackageName}.${DOMAIN_CLASSNAME}" >
     <#list modelColumn as model>
     <#if model.columnName='id'>
        <id column="id" property="id"/>
     <#else>
        <result column="${model.columnName}" property="${model.changeColumnName}" />
     </#if>
     </#list>
    </resultMap>
  
    <!-- base查询字段  -->
    <sql id="baseColumnList" >
        <trim suffixOverrides="," >
            <#if modelColumn?exists>
                <#list modelColumn as model>
            t.${model.columnName},
                </#list>
            </#if>
        </trim>
    </sql>

    <!-- base paging where条件  -->
    <sql id="baseWhereCondition" >
        <#if modelColumn?exists>
        <where>
          <#list modelColumn as model>
              <#if model.modelAttrType='JSONObject'>
            <if test="${model.changeColumnName}Json != null ">
                <foreach collection="${model.changeColumnName}Json" index="key" item="value">
                  <choose>
                      <when test="key == 'xxxDemoKey' and value != null">
                          and json_extract(${model.columnName}, CONCAT('$.',#${r'{'}key${r'}'})) > #${r'{'}value${r'}'}
                      </when>
                      <otherwise>
                          <if test="value != null">
                              and json_extract(${model.columnName}, CONCAT('$.',#${r'{'}key})) = #${r'{'}value${r'}'}
                          </if>
                      </otherwise>
                  </choose>
                </foreach>
            </if>
              <#else>
            <if test="${model.changeColumnName} != null ">
                and t.${model.columnName} = #${r'{'}${model.changeColumnName}${r'}'}
            </if>
              </#if>
          </#list>
        </where>
        </#if>
    </sql>

    <!--新增${tableAnnotation}-->
    <insert id="create${DOMAIN_CLASSNAME}" parameterType="${setDomainPackageName}.${DOMAIN_CLASSNAME}" useGeneratedKeys="true" keyProperty="id">
        insert into ${tableNameSmall}
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <#if modelColumn?exists>
                <#list modelColumn as model>
            <if test="${model.changeColumnName} != null ">
                ${model.columnName},
            </if>
                </#list>
            </#if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <#if modelColumn?exists>
                <#list modelColumn as model>
            <if test="${model.changeColumnName} != null ">
                #${r'{'}${model.changeColumnName}${r'}'},
            </if>
                </#list>
            </#if>
        </trim>
    </insert>

    <!--根据id修改${tableAnnotation}-->
    <update id="update${DOMAIN_CLASSNAME}ById" parameterType="${setDomainPackageName}.${DOMAIN_CLASSNAME}">
        update ${tableNameSmall}
        <set>
            <#if modelColumn?exists>
                <#list modelColumn as model>
                    <#if model.changeColumnName != 'id'>
                        <#if model.modelAttrType='--JSONObject'>
            <if test="${model.changeColumnName}Json != null ">
                <foreach collection="${model.changeColumnName}Json" index="key" item="value">
                    <if test="value != null">
                        ${model.columnName} = json_set(${model.columnName}, CONCAT('$.',#${r'{'}key${r'}'}),#${r'{'}value${r'}'}),
                    </if>
                </foreach>
            </if>
                        <#else>
            <if test="${model.changeColumnName} != null<#--<#if model.columnType='VARCHAR'> and ${model.changeColumnName} != '' </#if>--> ">
                ${model.columnName} = #${r'{'}${model.changeColumnName}${r'}'},
            </if>
                         </#if>
                    </#if>
                </#list>
            </#if>
        </set>
        where id=#${r'{'}id${r'}'}
    </update>

    <!--根据id删除${tableAnnotation}-->
    <delete id="delete${DOMAIN_CLASSNAME}ById" parameterType="${primaryKeyModtype}">
        delete from ${tableNameSmall} where id = #${r'{'}id${r'}'}
    </delete>

    <!--根据ids删除${tableAnnotation}-->
    <delete id="delete${DOMAIN_CLASSNAME}ByIds" parameterType="java.util.List">
        delete from ${tableNameSmall} where id in
        <foreach collection="list" item="item" open="("
            separator="," close=")">
            #${r'{'}item${r'}'}
        </foreach>
    </delete>

    <!-- 根据id查找${tableAnnotation} -->
    <select id="select${DOMAIN_CLASSNAME}ById" resultMap="baseResultMap" parameterType="${primaryKeyModtype}">
        select
            <include refid="baseColumnList" />
        from
            ${tableNameSmall} t
        where t.${primaryKey} = #${r'{'}id${r'}'}
    </select>

    <!--查询${tableAnnotation}分页列表-->
    <select id="select${DOMAIN_CLASSNAME}List" resultMap="baseResultMap" parameterType="${setDomainPackageName}.${DOMAIN_CLASSNAME}">
        select
            <include refid="baseColumnList" />
        from
            ${tableNameSmall} t
        <include refid="baseWhereCondition" />
    </select>
</mapper>